package com.adinnet.admin.common.shiro;

import com.adinnet.admin.system.model.SysMenu;
import com.adinnet.admin.system.model.SysUser;
import com.adinnet.admin.system.model.vo.SysUserRoleVo;
import com.adinnet.admin.system.service.SysMenuService;
import com.adinnet.admin.system.service.SysUserRoleService;
import com.adinnet.admin.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Shiro认证和授权
 * Created by wangfan on 2018-02-22 上午 11:29
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    @Lazy
    private SysUserService sysUserService;
    @Autowired
    @Lazy
    private SysUserRoleService sysUserRoleService;
    @Autowired
    @Lazy
    private SysMenuService sysMenuService;

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // 角色
        List<SysUserRoleVo> userRoles=sysUserRoleService.getRoles(user.getId());
        Set<String> roles = new HashSet<>();
        for (int i = 0; i < userRoles.size(); i++) {
            roles.add(String.valueOf(userRoles.get(i).getRoleCode()));
        }
        authorizationInfo.setRoles(roles);
        // 权限
        List<SysMenu> sysMenus = sysMenuService.listByUserId(user.getId());
        Set<String> permissions = new HashSet<>();
        for (int i = 0; i < sysMenus.size(); i++) {
            String permission = sysMenus.get(i).getPermission();
            if (StringUtils.isNotBlank(permission)) {
                permissions.add(permission);
            }
        }
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        SysUser user = sysUserService.getByUsername(username);
        if (user == null) {
            throw new UnknownAccountException(); // 账号不存在
        }
        if (user.getStatus() != 0) {
            throw new LockedAccountException("账号已被锁定");  // 账号被锁定
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(EndecryptUtil.DEFAULT_SALT), getName());
        return authenticationInfo;
    }
}
